﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;

namespace WarehouseManager.Common
{
    public class Print
    {
        #region 打印相关的处理
        private int nBuShu = 1;
        public int BuShu
        {
            get { return nBuShu; }
            set { nBuShu = value; }
        }
        private Microsoft.Office.Interop.Excel.Workbook Backup(ref Microsoft.Office.Interop.Excel.Workbooks books,string template)
        {
            string strTemplateFilePath = System.Environment.CurrentDirectory + @"\" + template;
            if (System.IO.File.Exists(strTemplateFilePath) == false)
            {
                MessageBoxHelp.ShowMsgError("请检查模板文件：" + strTemplateFilePath + "   是否存在");
                return null;
            }
            try
            {
                Microsoft.Office.Interop.Excel.Workbook book = books.Open(strTemplateFilePath, Type.Missing, true,
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                Microsoft.Office.Interop.Excel.Worksheet sheetReserved = book.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
                string strSheetName = sheetReserved.Name;
                foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in book.Worksheets)
                {
                    if (sheet.Name != strSheetName)
                    {
                        sheet.Delete();
                    }
                }
                string steTempFile = GetTempFileName(template);

                book.SaveAs(steTempFile, System.Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                return book;
            }
            catch
            {
                return null;
            }
        }
        private string GetTempFileName(string strTemplateFile)
        {
            StringBuilder sb = new StringBuilder(System.IO.Path.GetTempPath());
            sb.Append(System.IO.Path.GetFileNameWithoutExtension(strTemplateFile));
            DateTime dtNow = DateTime.Now;
            sb.Append(dtNow.Month.ToString());
            sb.Append(dtNow.Day.ToString());
            sb.Append(dtNow.Hour.ToString());
            sb.Append(dtNow.Minute.ToString());
            sb.Append(dtNow.Second.ToString());
            sb.Append(dtNow.Millisecond.ToString());
            sb.Append(System.IO.Path.GetExtension(strTemplateFile));
            return sb.ToString();
        }

        private void SetCellTitleForPaient(Microsoft.Office.Interop.Excel.Worksheet sheet, List<string> title)
        {
            sheet.Cells[4, 4] = title[0];
            sheet.Cells[4, 17] = title[1];
            sheet.Cells[5, 4] = title[2];
            sheet.Cells[5, 17] = title[3];
            sheet.Cells[6, 4] = title[4];
            sheet.Cells[6, 17] = title[5];
         }
        private void SetCellTitleBillHistory(Microsoft.Office.Interop.Excel.Worksheet sheet, List<string> title)
        {
            sheet.Cells[4, 16] = title[0];
            sheet.Cells[4, 5] = title[1] + " - " + title[2];

        }
        private void SetCellTitlePaint(Microsoft.Office.Interop.Excel.Worksheet sheet, List<string> title)
        {
            sheet.Cells[4, 4] = title[0];
            sheet.Cells[4, 17] = title[1];
            sheet.Cells[5, 4] = title[2];
            sheet.Cells[5, 17] = title[3];
            sheet.Cells[6, 4] = title[4];
            sheet.Cells[6, 17] = title[5];
        }
        private void SetCellValuePaint(Microsoft.Office.Interop.Excel.Worksheet sheet, int firstrow, int ntempIndex, int index, DataTable table)
        {
            sheet.Cells[firstrow + ntempIndex, 2] = table.Rows[index][0];
            sheet.Cells[firstrow + ntempIndex, 3] = table.Rows[index][1];
            sheet.Cells[firstrow + ntempIndex, 5] = table.Rows[index][2];
            sheet.Cells[firstrow + ntempIndex, 12] = table.Rows[index][3];
            sheet.Cells[firstrow + ntempIndex, 16] = table.Rows[index][4];
            sheet.Cells[firstrow + ntempIndex, 26] = table.Rows[index][5];
            sheet.Cells[firstrow + ntempIndex, 27] = table.Rows[index][6];
        }

        #region 废弃代码
        //private void SetCellTitle(Microsoft.Office.Interop.Excel.Worksheet sheet, List<string> title)
        //{
        //    sheet.Cells[7, 4] = title[0];
        //    sheet.Cells[7, 7] = title[1];
        //    sheet.Cells[8, 4] = title[2];
        //    sheet.Cells[8, 7] = title[3];
        //    sheet.Cells[9, 4] = title[4];
        //    sheet.Cells[9, 7] = title[5];

        //    sheet.Cells[32, 4] = title[0];
        //    sheet.Cells[32, 7] = title[1];
        //    sheet.Cells[33, 4] = title[2];
        //    sheet.Cells[33, 7] = title[3];
        //    sheet.Cells[34, 4] = title[4];
        //    sheet.Cells[34, 7] = title[5];
        //}
          //private void SetCellValue(Microsoft.Office.Interop.Excel.Worksheet sheet, int firstrow, int ntempIndex, int index, DataTable table)
        //{
        //    sheet.Cells[firstrow  + ntempIndex, 2] = table.Rows[index][0];
        //    sheet.Cells[firstrow  + ntempIndex, 3] = table.Rows[index][1];
        //    sheet.Cells[firstrow  + ntempIndex, 4] = table.Rows[index][2];
        //    sheet.Cells[firstrow  + ntempIndex, 6] = table.Rows[index][3];
        //    sheet.Cells[firstrow  + ntempIndex, 7] = table.Rows[index][4];
        //    sheet.Cells[firstrow  + ntempIndex, 9] = table.Rows[index][5];
        //    sheet.Cells[firstrow  + ntempIndex, 10] = table.Rows[index][6];


        //    sheet.Cells[firstrow + 25 + ntempIndex, 2] = table.Rows[index][0];
        //    sheet.Cells[firstrow + 25 + ntempIndex, 3] = table.Rows[index][1];
        //    sheet.Cells[firstrow + 25 + ntempIndex, 4] = table.Rows[index][2];
        //    sheet.Cells[firstrow + 25 + ntempIndex, 6] = table.Rows[index][3];
        //    sheet.Cells[firstrow + 25 + ntempIndex, 7] = table.Rows[index][4];
        //    sheet.Cells[firstrow + 25 + ntempIndex, 9] = table.Rows[index][5];
        //    sheet.Cells[firstrow + 25 + ntempIndex, 10] = table.Rows[index][6];
        //}
        //private void SetCellValueForPaient(Microsoft.Office.Interop.Excel.Worksheet sheet, int firstrow, int ntempIndex, int index, DataTable table)
        //{
        //    sheet.Cells[firstrow + ntempIndex, 2] = table.Rows[index][0];
        //    sheet.Cells[firstrow + ntempIndex, 3] = table.Rows[index][1];
        //    sheet.Cells[firstrow + ntempIndex, 5] = table.Rows[index][2];
        //    sheet.Cells[firstrow + ntempIndex, 6] = table.Rows[index][3];
        //    sheet.Cells[firstrow + ntempIndex, 16] = table.Rows[index][4];
        //    sheet.Cells[firstrow + ntempIndex, 26] = table.Rows[index][5];
        //    sheet.Cells[firstrow + ntempIndex, 27] = table.Rows[index][6];
        //}

        ///// <summary>
        ///// table Columns(0：序号 1：货号 2：品名 3：规格 4：生产企业 5：数量 6：生产批号) Count = 7
        ///// title node（0：患者名称: 1：详细地址 2：联系电话 3：缴费日期 4：医院经办人 5：送货日期）Count = 5
        ///// </summary>
        ///// <param name="table">打印数据</param>
        ///// <param name="title">title信息</param>
        //public bool DoPrint(DataTable table, List<string> title)
        //{
        //    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
        //    excel.Visible = false;
        //    excel.UserControl = true;
        //    Microsoft.Office.Interop.Excel.Workbooks books = excel.Workbooks;
        //    Microsoft.Office.Interop.Excel._Workbook book = null;
        //    if ((book = Backup(ref books, "BillTemplate.xls")) == null)
        //    {
        //        books.Close();
        //        excel.Quit();
        //        ReleaseObjectRef(books);
        //        ReleaseObjectRef(excel);
        //        GC.Collect();
        //        return false;
        //    }
        //    int nAllRowCount = table.Rows.Count;
        //    string strTemp = book.FullName;
        //    Microsoft.Office.Interop.Excel.Worksheet sheet = book.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
        //    sheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlPortrait;
            
        //    int nFirstRow = 12;
        //    int nIndex = 0;
        //    int nPageCount = nAllRowCount / 10;
        //    int nLastPageRowCount = nAllRowCount % 10 == 0 ? 10 : nAllRowCount % 10;
        //    if (nAllRowCount % 10 != 0)
        //    {
        //        nPageCount++;
        //    }
        //    try
        //    {
        //        for (int nCountTemp = 1; nCountTemp <= nPageCount; nCountTemp++)
        //        {
        //            if (nCountTemp != nPageCount)
        //            {
        //                for (int i = 0; i < 11; i++)
        //                {
        //                    if (i < 10)
        //                    {
        //                        if (i == 0)
        //                        {
        //                            SetCellTitle(sheet, title);
        //                        }
        //                        SetCellValue(sheet, nFirstRow, i, nIndex, table);
        //                        nIndex++;
        //                    }
        //                    else
        //                    {
        //                        //sheet.Cells[23, 1] = "第" + nCountTemp.ToString() + "页" + "   共" + nPageCount.ToString() + "页";
        //                        //sheet.Cells[58, 1] = "第" + nCountTemp.ToString() + "页" + "   共" + nPageCount.ToString() + "页";
        //                    }
        //                }
        //                book.PrintOut(Missing.Value, Missing.Value, nBuShu, Missing.Value,
        //                    Missing.Value, Missing.Value, true, Missing.Value);
        //            }
        //            // 最后一页打印
        //            else
        //            {
        //                if (nLastPageRowCount > 0)
        //                {
        //                    for (int i = 0; i < 11; i++)
        //                    {
        //                        if (i < nLastPageRowCount)
        //                        {
        //                            if (i == 0)
        //                            {
        //                                SetCellTitle(sheet, title);
        //                            }
        //                            SetCellValue(sheet, nFirstRow, i, nIndex, table);
        //                            nIndex++;

        //                        }
        //                        else if (i < 10)
        //                        {
        //                            sheet.Cells[nFirstRow + i, 2] = "";
        //                            sheet.Cells[nFirstRow + i, 3] = "";
        //                            sheet.Cells[nFirstRow + i, 4] = "";
        //                            sheet.Cells[nFirstRow + i, 6] = "";
        //                            sheet.Cells[nFirstRow + i, 7] = "";
        //                            sheet.Cells[nFirstRow + i, 9] = "";
        //                            sheet.Cells[nFirstRow + i, 10] = "";

        //                            sheet.Cells[nFirstRow + 25 + i, 2] = "";
        //                            sheet.Cells[nFirstRow + 25 + i, 3] = "";
        //                            sheet.Cells[nFirstRow + 25 + i, 4] = "";
        //                            sheet.Cells[nFirstRow + 25 + i, 6] = "";
        //                            sheet.Cells[nFirstRow + 25 + i, 7] = "";
        //                            sheet.Cells[nFirstRow + 25 + i, 9] = "";
        //                            sheet.Cells[nFirstRow + 25 + i, 10] = "";
        //                        }
        //                    }
        //                    //sheet.Cells[23, 1] = "第" + nCountTemp.ToString() + "页" + "   共" + nPageCount.ToString() + "页";
        //                    //sheet.Cells[58, 1] = "第" + nCountTemp.ToString() + "页" + "   共" + nPageCount.ToString() + "页";
        //                    book.PrintOut(Missing.Value, Missing.Value, nBuShu, Missing.Value, Missing.Value, Missing.Value, true, Missing.Value);
                            
        //                }
        //            }
        //        }
        //    }
        //    finally
        //    {
        //        book.Close(false, Type.Missing, Type.Missing);
        //        books.Close();
        //        excel.Quit();
        //        ReleaseObjectRef(sheet);
        //        sheet = null;
        //        ReleaseObjectRef(book);
        //        book = null;
        //        ReleaseObjectRef(books);
        //        books = null;
        //        ReleaseObjectRef(excel);
        //        excel = null;
        //        GC.Collect();
        //        ClearBackup(strTemp);
        //    }
        //    return true;
        //}
        #endregion 废弃代码

        private void SetCellValueBillHistory(Microsoft.Office.Interop.Excel.Worksheet sheet, int firstrow, int ntempIndex, int index, DataTable table)
        {
            sheet.Cells[firstrow + ntempIndex, 3] = table.Rows[index][0];
            sheet.Cells[firstrow + ntempIndex, 5] = table.Rows[index][1];
            sheet.Cells[firstrow + ntempIndex, 7] = table.Rows[index][2];
            sheet.Cells[firstrow + ntempIndex, 9] = table.Rows[index][3];
            sheet.Cells[firstrow + ntempIndex, 11] = table.Rows[index][4];
            sheet.Cells[firstrow + ntempIndex, 13] = table.Rows[index][5];
            sheet.Cells[firstrow + ntempIndex, 15] = table.Rows[index][6];
        }
        private void ClearBackup(string fileName)
        {
            try
            {
                // Delete the file specified by user
                System.IO.File.Delete(fileName);
            }
            catch { }
        }
        private void ReleaseObjectRef(object o)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
            }
            catch
            { }
            finally
            {
                o = null;
            }
        }

        
        #region DoPrint
        /// <summary>
        /// table Columns(0：序号 1：货号 2：品名 3：规格 4：生产企业 5：数量 6：生产批号) Count = 7
        /// title node（0：患者名称: 1：详细地址 2：联系电话 3：缴费日期 4：医院经办人 5：送货日期）Count = 5
        /// </summary>
        /// <param name="table">打印数据</param>
        /// <param name="title">title信息</param>
        public bool DoPrint(DataTable table, List<string> title)
        {
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            excel.Visible = false;
            excel.UserControl = true;
            Microsoft.Office.Interop.Excel.Workbooks books = excel.Workbooks;
            Microsoft.Office.Interop.Excel._Workbook book = null;
            if ((book = Backup(ref books, "BillTemplate.xls")) == null)
            {
                books.Close();
                excel.Quit();
                ReleaseObjectRef(books);
                ReleaseObjectRef(excel);
                GC.Collect();
                return false;
            }
            int nAllRowCount = table.Rows.Count;
            string strTemp = book.FullName;
            Microsoft.Office.Interop.Excel.Worksheet sheet = book.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
            sheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlPortrait;

            int nFirstRow = 9;
            int nIndex = 0;
            int nPageCount = nAllRowCount / 15;
            int nLastPageRowCount = nAllRowCount % 15 == 0 ? 15 : nAllRowCount % 15;
            if (nAllRowCount % 15 != 0)
            {
                nPageCount++;
            }
            try
            {
                for (int nCountTemp = 1; nCountTemp <= nPageCount; nCountTemp++)
                {
                    if (nCountTemp != nPageCount)
                    {
                        for (int i = 0; i < 16; i++)
                        {
                            if (i < 15)
                            {
                                if (i == 0)
                                {
                                    SetCellTitlePaint(sheet, title);
                                }
                                SetCellValuePaint(sheet, nFirstRow, i, nIndex, table);
                                nIndex++;
                            }
                            else
                            {
                                //sheet.Cells[23, 1] = "第" + nCountTemp.ToString() + "页" + "   共" + nPageCount.ToString() + "页";
                                //sheet.Cells[58, 1] = "第" + nCountTemp.ToString() + "页" + "   共" + nPageCount.ToString() + "页";
                            }
                        }
                        book.PrintOut(Missing.Value, Missing.Value, nBuShu, Missing.Value,
                            Missing.Value, Missing.Value, true, Missing.Value);
                    }
                    // 最后一页打印
                    else
                    {
                        if (nLastPageRowCount > 0)
                        {
                            for (int i = 0; i < 16; i++)
                            {
                                if (i < nLastPageRowCount)
                                {
                                    if (i == 0)
                                    {
                                        SetCellTitlePaint(sheet, title);
                                    }
                                    SetCellValuePaint(sheet, nFirstRow, i, nIndex, table);
                                    nIndex++;

                                }
                                else if (i < 15)
                                {
                                    sheet.Cells[nFirstRow + i, 2] = "";
                                    sheet.Cells[nFirstRow + i, 3] = "";
                                    sheet.Cells[nFirstRow + i, 5] = "";
                                    sheet.Cells[nFirstRow + i, 12] = "";
                                    sheet.Cells[nFirstRow + i, 16] = "";
                                    sheet.Cells[nFirstRow + i, 26] = "";
                                    sheet.Cells[nFirstRow + i, 27] = "";
                                }
                            }
                            //sheet.Cells[23, 1] = "第" + nCountTemp.ToString() + "页" + "   共" + nPageCount.ToString() + "页";
                            //sheet.Cells[58, 1] = "第" + nCountTemp.ToString() + "页" + "   共" + nPageCount.ToString() + "页";
                            book.PrintOut(Missing.Value, Missing.Value, nBuShu, Missing.Value, Missing.Value, Missing.Value, true, Missing.Value);

                        }
                    }
                }
            }
            finally
            {
                book.Close(false, Type.Missing, Type.Missing);
                books.Close();
                excel.Quit();
                ReleaseObjectRef(sheet);
                sheet = null;
                ReleaseObjectRef(book);
                book = null;
                ReleaseObjectRef(books);
                books = null;
                ReleaseObjectRef(excel);
                excel = null;
                GC.Collect();
                ClearBackup(strTemp);
            }
            return true;
        }
        #endregion DoPrint

        #region DoPrintForPaient
        public bool DoPrintForPaient(DataTable table, List<string> title)
        {
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            excel.Visible = false;
            excel.UserControl = true;
            Microsoft.Office.Interop.Excel.Workbooks books = excel.Workbooks;
            Microsoft.Office.Interop.Excel._Workbook book = null;
            if ((book = Backup(ref books, "BillTemplatePaient.xls")) == null)
            {
                books.Close();
                excel.Quit();
                ReleaseObjectRef(books);
                ReleaseObjectRef(excel);
                GC.Collect();
                return false;
            }
            int nAllRowCount = table.Rows.Count;
            string strTemp = book.FullName;
            Microsoft.Office.Interop.Excel.Worksheet sheet = book.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
            sheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlPortrait;

            int nFirstRow = 9;
            int nIndex = 0;
            int nPageCount = nAllRowCount / 7;
            int nLastPageRowCount = nAllRowCount % 7 == 0 ? 7 : nAllRowCount % 7;
            if (nAllRowCount % 7 != 0)
            {
                nPageCount++;
            }
            try
            {
                for (int nCountTemp = 1; nCountTemp <= nPageCount; nCountTemp++)
                {
                    if (nCountTemp != nPageCount)
                    {
                        for (int i = 0; i < 8; i++)
                        {
                            if (i < 7)
                            {
                                if (i == 0)
                                {
                                    SetCellTitlePaint(sheet, title);
                                }
                                SetCellValuePaint(sheet, nFirstRow, i, nIndex, table);
                                nIndex++;
                            }
                            else
                            {
                                //sheet.Cells[37, 3] = "第" + nCountTemp.ToString() + "页" + "   共" + nPageCount.ToString() + "页";
                            }
                        }
                        book.PrintOut(Missing.Value, Missing.Value, nBuShu, Missing.Value,
                            Missing.Value, Missing.Value, true, Missing.Value);
                    }
                    // 最后一页打印
                    else
                    {
                        if (nLastPageRowCount > 0)
                        {
                            for (int i = 0; i < 8; i++)
                            {
                                if (i < nLastPageRowCount)
                                {
                                    if (i == 0)
                                    {
                                        SetCellTitlePaint(sheet, title);
                                    }
                                    SetCellValuePaint(sheet, nFirstRow, i, nIndex, table);
                                    nIndex++;

                                }
                                else if (i < 7)
                                {
                                    sheet.Cells[nFirstRow + i, 2] = "";
                                    sheet.Cells[nFirstRow + i, 3] = "";
                                    sheet.Cells[nFirstRow + i, 4] = "";
                                    sheet.Cells[nFirstRow + i, 12] = "";
                                    sheet.Cells[nFirstRow + i, 16] = "";
                                    sheet.Cells[nFirstRow + i, 26] = "";
                                    sheet.Cells[nFirstRow + i, 27] = "";

                                }
                            }
                            //sheet.Cells[37, 3] = "第" + nCountTemp.ToString() + "页" + "   共" + nPageCount.ToString() + "页";
                            book.PrintOut(Missing.Value, Missing.Value, nBuShu, Missing.Value, Missing.Value, Missing.Value, true, Missing.Value);

                        }
                    }
                }
            }
            finally
            {
                book.Close(false, Type.Missing, Type.Missing);
                books.Close();
                excel.Quit();
                ReleaseObjectRef(sheet);
                sheet = null;
                ReleaseObjectRef(book);
                book = null;
                ReleaseObjectRef(books);
                books = null;
                ReleaseObjectRef(excel);
                excel = null;
                GC.Collect();
                ClearBackup(strTemp);
            }
            return true;

        }
        #endregion DoPrintForPaient

        #region DoPrintBillHistory
        /// <summary>
        /// 
        /// </summary>
        /// <param name="table"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public bool DoPrintBillHistory(DataTable table, List<string> title)
        {
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            excel.Visible = false;
            excel.UserControl = true;
            Microsoft.Office.Interop.Excel.Workbooks books = excel.Workbooks;
            Microsoft.Office.Interop.Excel._Workbook book = null;
            if ((book = Backup(ref books, "BillHistoryTemplate.xls")) == null)
            {
                books.Close();
                excel.Quit();
                ReleaseObjectRef(books);
                ReleaseObjectRef(excel);
                GC.Collect();
                return false;
            }
            int nAllRowCount = table.Rows.Count;
            string strTemp = book.FullName;
            Microsoft.Office.Interop.Excel.Worksheet sheet = book.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
            sheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;

            int nFirstRow = 8;
            int nIndex = 0;
            int nPageCount = nAllRowCount / 28;
            int nLastPageRowCount = nAllRowCount % 28 == 0 ? 28 : nAllRowCount % 28;
            if (nAllRowCount % 28 != 0)
            {
                nPageCount++;
            }
            try
            {
                for (int nCountTemp = 1; nCountTemp <= nPageCount; nCountTemp++)
                {
                    if (nCountTemp != nPageCount)
                    {
                        for (int i = 0; i < 29; i++)
                        {
                            if (i < 28)
                            {
                                if (i == 0)
                                {
                                    SetCellTitleBillHistory(sheet, title);
                                }
                                SetCellValueBillHistory(sheet, nFirstRow, i, nIndex, table);
                                nIndex++;
                            }
                            else
                            {
                                sheet.Cells[37, 3] = "第" + nCountTemp.ToString() + "页" + "   共" + nPageCount.ToString() + "页";
                            }
                        }
                        book.PrintOut(Missing.Value, Missing.Value, nBuShu, Missing.Value,
                            Missing.Value, Missing.Value, true, Missing.Value);
                    }
                    // 最后一页打印
                    else
                    {
                        if (nLastPageRowCount > 0)
                        {
                            for (int i = 0; i < 29; i++)
                            {
                                if (i < nLastPageRowCount)
                                {
                                    if (i == 0)
                                    {
                                        SetCellTitleBillHistory(sheet, title);
                                    }
                                    SetCellValueBillHistory(sheet, nFirstRow, i, nIndex, table);
                                    nIndex++;

                                }
                                else if (i < 28)
                                {
                                    sheet.Cells[nFirstRow + i, 3] = "";
                                    sheet.Cells[nFirstRow + i, 5] = "";
                                    sheet.Cells[nFirstRow + i, 7] = "";
                                    sheet.Cells[nFirstRow + i, 9] = "";
                                    sheet.Cells[nFirstRow + i, 11] = "";
                                    sheet.Cells[nFirstRow + i, 13] = "";
                                    sheet.Cells[nFirstRow + i, 15] = "";
                                }
                            }
                            sheet.Cells[37, 3] = "第" + nCountTemp.ToString() + "页" + "   共" + nPageCount.ToString() + "页";
                            book.PrintOut(Missing.Value, Missing.Value, nBuShu, Missing.Value, Missing.Value, Missing.Value, true, Missing.Value);

                        }
                    }
                }
            }
            finally
            {
                book.Close(false, Type.Missing, Type.Missing);
                books.Close();
                excel.Quit();
                ReleaseObjectRef(sheet);
                sheet = null;
                ReleaseObjectRef(book);
                book = null;
                ReleaseObjectRef(books);
                books = null;
                ReleaseObjectRef(excel);
                excel = null;
                GC.Collect();
                ClearBackup(strTemp);
            }
            return true;

        }
        #endregion DoPrintBillHistory

        #endregion 打印的相关处理
    }
}
